<?
class Date{

	public static function thai_date($date = "", $short = false){
		if($date == ""){
			$date = time();
		}

		return date("d", $date). " " .Date::thai_month(date("m",$date), $short). " " . Date::thai_year(date("Y",$date), $short);
	}

	public static function thai_datetime($date = "", $short = false){

		if($date == ""){
			$date = time();
		}

		if($short){
			return date("H:i", $date) ." น. ". date("d", $date). " " .Date::thai_month(date("m",$date), $short). " " . Date::thai_year(date("Y",$date), $short);
		}else{
			return date("H:i:s", $date) ." - ". date("d", $date). " " .Date::thai_month(date("m",$date), $short). " " . Date::thai_year(date("Y",$date), $short);
		}
	}

	public static function thai_pasttime($date, $point){
		$diff = time() - $date;

		if($diff >= $point){
			return Date::thai_date($date);
		}else{
			$text = "ผ่านมาแล้ว ";

			if($diff < 60)
				return $text . $diff . " วินาที";
			elseif($diff < 3600)
				return $text . floor($diff/60) . " นาที ". ($diff%60) ." วินาที";
			elseif($diff < 86400)
				return $text . floor($diff/3600) . " ชั่วโมง ". floor(($diff%3600)/60) . " นาที ". ($diff%60) ." วินาที";
			elseif($diff < $point)
				return $text . floor($diff/86400). " วัน ".floor(($diff%86400)/3600) . " ชั่วโมง ". floor(($diff%3600)/60) . " นาที ". ($diff%60) ." วินาที";

			return $text;
		}
	}

	public static function thai_year($y, $short = false){
		if($short){
			return substr($y + 543, 2, 2);
		}
		return $y + 543;
	}

	public static function thai_month($m, $short = false){
		if($short){
			if($m == 1) return "ม.ค.";
			if($m == 2) return "ก.พ.";
			if($m == 3) return "มี.ค.";
			if($m == 4) return "เม.ย.";
			if($m == 5) return "พ.ค.";
			if($m == 6) return "มิ.ย.";
			if($m == 7) return "ก.ค.";
			if($m == 8) return "ส.ค.";
			if($m == 9) return "ก.ย.";
			if($m == 10) return "ต.ค.";
			if($m == 11) return "พ.ย.";
			if($m == 12) return "ธ.ค.";
		}else{
			if($m == 1) return "มกราคม";
			if($m == 2) return "กุมภาพันธ์";
			if($m == 3) return "มีนาคม";
			if($m == 4) return "เมษายน";
			if($m == 5) return "พฤษภาคม";
			if($m == 6) return "มิถุนายน";
			if($m == 7) return "กรกฎาคม";
			if($m == 8) return "สิงหาคม";
			if($m == 9) return "กันยายน";
			if($m == 10) return "ตุลาคม";
			if($m == 11) return "พฤศจิกายน";
			if($m == 12) return "ธันวาคม";
		}
	}

	public static function group_date($s, $e){

		if($e - $s < DAY) {
			return date("d M Y",$s);
		}elseif(date("m",$s) == date("m",$e)){
			return date("d", $s) . " - ". date("d", $e)." ". date("M Y",$s);
		}elseif(date("y",$s) == date("y",$e)){
			return date("d M", $s) . " - ". date("d M", $e)." ". date("Y",$s);
		}else{
			return date("d M Y", $s) . " - ". date("d M Y", $e);
		}
	}
}

?>